package com.uflower.user.web;

import com.uflower.user.entity.CollectionEntity;
import com.uflower.user.entity.ReceiveAddressEntity;
import com.uflower.user.entity.UserEntity;
import com.uflower.user.interceptor.LoginUserInterceptor;
import com.uflower.user.service.CollectionService;
import com.uflower.user.service.ReceiveAddressService;
import com.uflower.user.service.UserService;
import com.uflower.user.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Api(tags = "前台用户登录和注册")
public class WebPageController {
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private UserService userService;
    @Autowired
    private ReceiveAddressService receiveAddressService;
    @ApiOperation("跳转到登录页面")
    @GetMapping({"/login", "login.html"})
    public String loginPage(Model model) {
        model.addAttribute("loginVo",new LoginVo());
        return "user/login";
    }
    @ApiOperation("跳转到注册页面")
    @GetMapping("/register")
    public String registerPage(){
        return "user/register";
    }
    @ApiOperation("跳转到登录错误页面")
    @GetMapping("/errorlogin")
    public String errorLoginPage(){
        return "error/error_login";
    }
    @ApiOperation("跳转到主页面")
    @GetMapping("/index")
    public String indexPage(){
        return "redirect:http://localhost:88/api/product/index";
    }
    @ApiOperation("跳转到个人信息页面")
    @GetMapping("/personal")
    public String personalPage(Model model){
        UserEntity user = userService.getById(LoginUserInterceptor.loginUser.get().getId());
        model.addAttribute("user",user);
        Map<String, Object> genders = new HashMap<String, Object>();
        genders.put("男", 1);
        genders.put("女", 0);
        model.addAttribute("genders", genders);
        System.out.println(user);
        return "user/personal";
    }
    @ApiOperation("跳转到收藏页面")
    @GetMapping("/collect")
    public String collectPage(Model model){
        List<CollectionEntity> collects = collectionService.getCollects(LoginUserInterceptor.loginUser.get().getId());
        model.addAttribute("collects",collects);
        return "user/collect";
    }
    @ApiOperation("跳转到收货地址页面")
    @GetMapping("/address")
    public String addressPage(Model model){
        System.out.println(LoginUserInterceptor.loginUser.get().getId());
        List<ReceiveAddressEntity> address = receiveAddressService.getAddress(LoginUserInterceptor.loginUser.get().getId());
        System.out.println(address);
        model.addAttribute("address",address);
        return "user/address";
    }
}
